const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const { connectDB } = require('./config/db');

// 导入路由
const examRoutes = require('./routes/examRoutes');
const resumeRoutes = require('./routes/resumeRoutes');
const interviewRoutes = require('./routes/interviewRoutes');
const planRoutes = require('./routes/plan');

// 加载环境变量
dotenv.config();

// 连接数据库
connectDB();

const app = express();
const PORT = process.env.PORT || 3001;

// 中间件
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// 基本路由
app.get('/', (req, res) => {
  res.json({ message: 'Welcome to Frontend Assistant API' });
});

// API 路由
app.get('/api/health', (req, res) => {
  res.json({ status: 'healthy', timestamp: new Date().toISOString() });
});

// 考试系统路由
app.use('/api/exam', examRoutes);

// 简历管理路由
app.use('/api/resume', resumeRoutes);

// 面试题库路由
app.use('/api/interview', interviewRoutes);

// 计划管理路由
app.use('/api', planRoutes);

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({ error: 'Something went wrong!' });
});

// 启动服务器
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
}); 